const baseURL = 'https://yangzhao.mynatapp.cc'

const httpInterceptor = {
	invoke(options) {
		// 1. 非 http 开头需拼接地址
		if (!options.url.startsWith('http')) {
			options.url = baseURL + options.url;
		}
		// 2. 请求超时
		options.timeout = 10000;
	}
}

uni.addInterceptor('request', httpInterceptor)
uni.addInterceptor('uploadFile', httpInterceptor)

export const http = (options) => {
	//返回promise对象
	return new Promise((resolve, reject) => {
		uni.request({
			...options,
			success(res) {
				if (res.statusCode >= 200 && res.statusCode < 300) {
					//提取res.data
					resolve(res.data)
				} else if (res.statusCode === 401) {


				} else {
					uni.showToast({
						icon: 'error',
						titile: '请求失败'
					})
					reject(res)
				}
			},
			fail(err) {
				uni.showToast({
					icon: 'error',
					title: '网络错误'
				})
				reject(err)
			},

		})
	})
}